/**
 * tzdesk系统平台
 * moonvip_admin
 * com.tz.service.good
 * IGoodService.java
 * 创建人:小胡
 * 时间：2016年12月06日 23:43:44
 *
 */
package com.tz.service.good.impl;

import com.tz.bean.*;
import com.tz.commonModel.GoodChildModel;
import com.tz.dao.good.IGoodMapper;
import com.tz.dao.goodchild.IGoodChildMapper;
import com.tz.dao.goodpicture.IGoodPictureMapper;
import com.tz.dao.goodscategories.IGoodsCategoriesMapper;
import com.tz.service.good.IGoodService;
import com.tz.util.StringHelper;
import com.tz.util.TmStringUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 平台商品管理serviceImpl
 * IGoodService
 * 创建人:小胡
 * 时间：2016年12月06日 23:43:44
 * @version 1.0.0
 *
 */
@Service
public class GoodServiceImpl implements IGoodService{

	@Resource
	private IGoodMapper goodMapper;

	@Resource
	private IGoodChildMapper goodChildMapper;
	@Resource
	private IGoodPictureMapper goodPictureMapper;
	@Resource
	private IGoodsCategoriesMapper goodsCategoriesMapper;

	private final  static  String key_suff =   "shop/goodcatogory/";
	private final  static  String key_suff2 =   "shop/good";


	@Override
	public List<Good> findGoods(TzParams params){
		if(TmStringUtils.isEmpty(params.getOrder()))params.setOrder("create_time desc");
		return goodMapper.findGoods(params);
	}

	@Override
	public int count(TzParams params) {
		return goodMapper.count(params);
	}

	@Override
	public HashMap<String ,Object> getGood(Integer id) {
		return goodMapper.getGood(id);
	}

	@Override
	public int save(Good good) {
		return goodMapper.save(good);
	}

	@Override
	public int update(Good good) {
		return goodMapper.update(good);
	}

	@Override
	public int delete(TzParams params) {
		return goodMapper.delete(params);
	}

	@Override
	public void saveGoodAndChild(Good good, Integer[] stocknumber, String[] color ,String[] urlArr) throws IOException {
//		//获得七牛商品图片外链
//		String goodBannerurl = StringHelper.uploadAndgetUrl(key_suff , good.getBannerurl() ,good.getCategoryId());
//		good.setBannerurl(goodBannerurl);
//		good.setDeleteCode("001");
//		goodMapper.save(good);
//		GoodChild goodChild = null;
//		for(int i=0;i<color.length;i++){
//			//获得七牛子商品图片外链
//			String imgurl = StringHelper.uploadAndgetUrl(key_suff , urlArr[i] , good.getCategoryId());
//			goodChild = new GoodChild();
//			goodChild.setColor(color[i]);
//			goodChild.setStocknumber(stocknumber[i]);
//			goodChild.setImgurl(imgurl);
//			goodChild.setDeleteCode("001");
//			goodChild.setGoodId(good.getId());
//			goodChildMapper.save(goodChild);
//		}
	}



	public void updateGoodAndChild(Good good , GoodChildModel goodChildModel) throws IOException {
//		if(good.getBannerurl()!=null && good.getBannerurl().indexOf("http://")==-1){
//			String goodBannerurl = StringHelper.uploadAndgetUrl(key_suff , good.getBannerurl() ,good.getCategoryId());
//			good.setBannerurl(goodBannerurl);
//		}
//		//先更新商品
//		good.setUpdateTime(new Date());
//		goodMapper.update(good);
//		List<GoodChild> goodChildList = goodChildModel.getGoodChildList();
//		if(goodChildList!=null){
//			for(GoodChild  item: goodChildList) {
//				if(item.getImgurl()!=null && item.getImgurl().indexOf("http://")==-1){
//					String imgurl = StringHelper.uploadAndgetUrl(key_suff , item.getImgurl() ,good.getCategoryId());
//					item.setImgurl(imgurl);
//				}
//				item.setUpdateTime(new Date());
//				if(item.getGoodChildId()==null ) {
//					item.setGoodId(good.getId());
//					goodChildMapper.save(item);
//				}else {
//					goodChildMapper.update(item);
//				}
//			}
//		}
	}


	/**
	 * 描述: 查询商品下已经绑定的所有分类
	 * 包名： com.tz.dao.role
	 * 类名：IRoleMapper
	 * 方法名: findPermissionByRoleId
	 * 参数类型: [roleId]
	 * 创建人：hufanglei
	 * 时间：2017/2/19   03:30
	 * qq：690328661
	 * @return java.lang.Integer[]
	 */
	public Integer[] findCatogoryByGoodId(Integer goodId){
		return goodMapper.findCatogoryByGoodId(goodId);

	}

	@Override
	public void saveGood(Good good, String[] detailimgurlArr, Integer[] catogoryIdArr, String goodchildStr) throws IOException {
		//保存商品
		String bannerurl = StringHelper.uploadAndgetUrl(key_suff2 , good.getBannerurl());//主图
		String hotimgurl = StringHelper.uploadAndgetUrl(key_suff2 , good.getHotimgurl());//推荐图
		good.setBannerurl(bannerurl);
		good.setHotimgurl(hotimgurl);
		int n = goodMapper.save(good);
		if(n<0){return;}
		GoodPicture goodPicture = null;
		//保存详情轮播图
		for (String imgurl:detailimgurlArr){
			imgurl =  StringHelper.uploadAndgetUrl(key_suff2 , imgurl);
			goodPicture = new GoodPicture();
			goodPicture.setGoodId(good.getId());
			goodPicture.setImgurl(imgurl);
			goodPictureMapper.save(goodPicture);
		}
		//保存商品子表
		//循环新增产品表
		//String sss = "[{\"color\":\"黄\",\"weight\":\"4.0g\",\"material\":\"K金\",\"price\":\"25\",\"stocknumber\":\"110\"},{\"color\":\"黄\",\"weight\":\"4.0g\",\"material\":\"黄金\",\"price\":\"35\",\"stocknumber\":\"230\"}]";
		JSONArray jsonArray= new JSONArray().fromObject(goodchildStr.replaceAll("&quot;","\"")); // 首先把字符串转成 JSONObect  对象，json 对象值使用""双引号存储
		GoodChild goodChild = null;
		for(int i=0,len=jsonArray.size();i<len;i++){
			goodChild = new GoodChild();
			JSONObject jsonObject = new JSONObject().fromObject(jsonArray.get(i));
			goodChild= (GoodChild) JSONObject.toBean(jsonObject, GoodChild.class);
			goodChild.setGoodId(good.getId());
			if(TmStringUtils.isNotEmpty(goodChild.getWeight())){
				String weight =   goodChild.getWeight().contains("g") ? goodChild.getWeight() : (goodChild.getWeight() + "g");
				goodChild.setWeight(weight);
			}
			goodChildMapper.save(goodChild);
		}
		//保存商品和分类中间表
		GoodsCategories goodsCategories = null;
		for(Integer catoId:catogoryIdArr){
			goodsCategories = new GoodsCategories();
			goodsCategories.setCategoryId(catoId);
			goodsCategories.setGoodId(good.getId());
			goodsCategoriesMapper.save(goodsCategories);
		}

	}


	/**
	 * 重新保存商品下面的所有分类
	 * @param goodId
	 * @param catogoriesStirng
	 */
	public String saveGoodAndCatogory(String goodId, String catogoriesStirng){
		//如果商品分类有，商品id不为空
		if (TmStringUtils.isNotEmpty(catogoriesStirng) && TmStringUtils.isNotEmpty(goodId)) {
			Integer gid = Integer.parseInt(goodId);
			//删除商品对应的分类
			goodMapper.deleteGoodAndCatogory(gid);
			//重新分配分类
			String[] users = catogoriesStirng.split(",");
			GoodsCategories goodsCategories = null;
			for (String string : users) {
				goodsCategories = new GoodsCategories();
				goodsCategories.setGoodId(gid);
				goodsCategories.setCategoryId(Integer.parseInt(string));
				goodsCategoriesMapper.save(goodsCategories);
			}
			return "success";
		} else {
			return "fail";

		}
	}

	//得到所有的轮播图片
	public List<GoodPicture> getAllPictures(Integer goodId){
		return  goodMapper.getAllPictures(goodId);
	}

	//更新商品的详情轮播图
	public String updateDetailImg(int[] delIds, String[] addImgs, Integer goodId) throws IOException {
		//添加图片
		String imgurl = null;
		GoodPicture goodPicture = null;
		for (String url:addImgs){
			if(!"0".equals(url)){
				url = url.replaceAll("&quot;","\"");
				imgurl =  StringHelper.uploadAndgetUrl(key_suff2 , url);
				goodPicture = new GoodPicture();
				goodPicture.setImgurl(imgurl);
				goodPicture.setGoodId(goodId);
				goodPictureMapper.save(goodPicture);
			}
		}
		//删除之前的图片
		for(int id:delIds){
			if(id!=0){
				goodPictureMapper.delete(id);
			}
		}
		return "ok";
	}


	public int updateGood(Good good) throws IOException {
		if(good.getBannerurl()!=null && good.getBannerurl().indexOf("http://")==-1){
			String bannerurl = StringHelper.uploadAndgetUrl(key_suff2 , good.getBannerurl());
			good.setBannerurl(bannerurl);
		}
		if(good.getHotimgurl()!=null && good.getHotimgurl().indexOf("http://")==-1){
			String hotimgurl = StringHelper.uploadAndgetUrl(key_suff2 , good.getHotimgurl());
			good.setHotimgurl(hotimgurl);
		}
		//先更新商品
		good.setUpdateTime(new Date());
		return  goodMapper.update(good);
	}

	/**
	 * 更新规格
	 * @param goodChild
	 * @return
	 */
	public int  updateSpec(GoodChild goodChild){
		goodChild.setUpdateTime(new Date());
		int n = goodChildMapper.update(goodChild);
		//如果修改的是库存，需要把商品的总库存修改
		if(null!=goodChild.getStocknumber() && n==1){
			int num = goodMapper.getALLStocknumById(goodChild.getGoodId());
			Good good = new Good();
			good.setId(goodChild.getGoodId());
			good.setStocknum(num);
			good.setUpdateTime(new Date());
			goodMapper.update(good);
		}
		return  n;
	}

	@Override
	public List<String> getColorByGoodId(Integer goodId) {
		return goodMapper.getColorByGoodId(goodId);
	}

	@Override
	public List<String> getWeightByGoodId(Integer goodId) {
		return goodMapper.getWeightByGoodId(goodId);
	}

	@Override
	public List<String> getMaterialByGoodId(Integer goodId) {
		return goodMapper.getMaterialByGoodId(goodId);
	}

	@Override
	public List<String> getMainstoreByGoodId(Integer goodId) {
		return goodMapper.getMainstoreByGoodId(goodId);
	}

	@Override
	public List<String> getHandSizeByGoodId(Integer goodId) {
		return goodMapper.getHandSizeByGoodId(goodId);
	}

	@Override
	public List<String> getNecklacelenByGoodId(Integer goodId) {
		return goodMapper.getNecklacelenByGoodId(goodId);
	}

	@Override
	public List<String> getBraceletlenByGoodId(Integer goodId) {
		return goodMapper.getBraceletlenByGoodId(goodId);
	}

	@Override
	public List<Double> getPriceByGoodId(Integer goodId) {
		return goodMapper.getPriceByGoodId(goodId);
	}

	@Override
	public List<Integer> getstockNumByGoodId(Integer goodId) {
		return goodMapper.getstockNumByGoodId(goodId);
	}


}
